package mya_dc.slave;

import mya_dc.shared_classes.externalExecuter.ILogDevice;
import mya_dc.shared_classes.externalExecuter.SysCommandExecutor;
import mya_dc.shared_classes.CompilationErrors;
import mya_dc.shared_classes.CompilationFlags;

/**
 * Compiles *.c* files located in a directory and creates object result file
 *
 * <p>
 * <b>Notes:</b>
 * <ul>
 * <li>Class uses Slave class "gcc" string that links to g++.exe 
 * </ul>
 * <p>
 * 
 * @author      Yuval Shimron
 * <br>			MYA
 */

class CompilationPackage
{	
	private String flags = null; 
	private String m_workingDirectory;
	
	/**
	 * Constructor
	 *
	 * @param	filesToCompileDirectory - The directory where files are stored
	 */	
	public CompilationPackage(String filesToCompileDirectory, CompilationFlags compFlags)
	{
		m_workingDirectory = filesToCompileDirectory;
		flags = " -c " + compFlags.toString() + " *.c*";
	}

	/**
	 * Compiles files in "m_workingDirectory" with flags "flags"
	 *
	 * @throws	Exception - might be thrown by the external command executer class
	 */
	CompilationErrors doCppCompilation() throws Exception
	{
		SysCommandExecutor cmdExecutor = new SysCommandExecutor();
		ILogDevice ErrorLog = new ILogDevice();
		cmdExecutor.setOutputLogDevice(null);
		cmdExecutor.setErrorLogDevice(ErrorLog);
		cmdExecutor.setWorkingDirectory(m_workingDirectory);
		
		cmdExecutor.runCommand(Slave.gcc + flags);
		
		return ErrorLog.m_Errors;
	}
}

